jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.serviceUnits.ServiceUnits", //the full name of the class to create
        jsx3.gui.LayoutGrid, //  extends
        null, // implements
        function (plugin) {
        	plugin.prototype.screenId;
        	plugin.prototype.serviceAssemblyId;
        
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	
        	plugin.serviceUnitsTableCacheId = "SPM_SERVICE_UNITS_TABLE_CACHE";
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
			plugin.showServiceUnitDetail = function(serviceUnitIdVal) {
        		var paramArray = { serviceUnitId:serviceUnitIdVal };
        		tibco.admin.monitormanage.general.NavigationHandler.publishMainPageNavigationMessage(tibco.admin.monitormanage.uicomponents.page.deployment.Deployment.serviceUnitScreenId, paramArray); 
        	}
        	
        	plugin.showNodeDetail = function(nodeIdVal) {
        		var paramArray = { nodeId:nodeIdVal };
        		tibco.admin.monitormanage.general.NavigationHandler.publishMainPageNavigationMessage(tibco.admin.monitormanage.uicomponents.page.gridInfrastructure.GridInfrastructure.nodeScreenId, paramArray); 
        	}
			
			plugin.handleHTMLEscaping = function(element,cdfkey, matrix, column, rownumber,server) {
				var strHTML = element.innerHTML;
				if(strHTML != ''){
					while(strHTML.indexOf("&lt;") >= 0)
					{ strHTML = strHTML.replace("&lt;","<"); }
					while(strHTML.indexOf("&gt;") >= 0)
					{ strHTML = strHTML.replace("&gt;",">"); }
					while(strHTML.indexOf("&quot;") >= 0)
					{ strHTML = strHTML.replace("&quot;","\""); }
					strHTML = strHTML.replace(/<a/g, "<a style='color:#257792'")
					element.innerHTML = strHTML;
				}
			}
			
			plugin.prototype.setUpComponent = function(screenId, serviceAssemblyId) {
				this.screenId = screenId;
				this.serviceAssemblyId = serviceAssemblyId;
				tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, this.handleDisplayScreen, this);
				this.callGetServiceUnits();
			}
			
			plugin.prototype.updateComponent = function() {
				this.callGetServiceUnits();
			}
			
			plugin.prototype.handleDisplayScreen = function(subject, message, tableObj) {
            	if(message.screenId == tableObj.screenId)
            	{
            		tableObj.serviceAssemblyId = message.serviceAssemblyId;
            		tableObj.callGetServiceUnits(); 
            	}
            }
			
    		//call this method to begin the service call (eg.service.callgetServiceUnits();)
    		plugin.prototype.callGetServiceUnits = function(serviceUnitId) {
      			if (tibco.admin.Main.getServer().getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/serviceUnits/rules/serviceUnitsRules.xml", "getServiceUnits");
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL("tibco/admin/monitormanage/uicomponents/table/serviceUnits/data/serviceUnitsData.xml");
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/serviceUnits/rules/serviceUnitsRules.xml"), "getServiceUnits", null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/serviceUnits/data/serviceUnitsData.xml"));
                }
                
                objService.serviceUnitsObj = this;
                objService.serviceAssemblyId = this.serviceAssemblyId;
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("status");
				metricsList.add("version");
				metricsList.add("type");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetServiceUnitsSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetServiceUnitsError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetServiceUnitsInvalid);
      			objService.doCall();
    		};

    		plugin.prototype.ongetServiceUnitsSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//jsx3.log("Success","The service call was successful.");
      			var cacheDoc = tibco.admin.Main.getServer().Cache.getDocument(plugin.serviceUnitsTableCacheId);
      			
      			// manipulate the cache for the "Nodes Mapped" column
      			var serviceUnitRecords = cacheDoc.getChildNodes();
      			for(i = 0; i < serviceUnitRecords.size(); i++)
      			{
      				var serviceUnitRecord = serviceUnitRecords.get(i);
      				var nodeInfoRecords = serviceUnitRecord.getChildNodes();
      				var nodesMappedText = "";
      				var nodeInfoRecordsSize = nodeInfoRecords.size();
      				for(j = 0; j < nodeInfoRecordsSize; j++)
      				{
      					var nodeInfoRecord = nodeInfoRecords.get(j);
      					nodesMappedText += "<a href=\"javascript:tibco.admin.monitormanage.uicomponents.table.serviceUnits.ServiceUnits.showNodeDetail('" + nodeInfoRecord.getAttribute("key") + "');\">" + nodeInfoRecord.getAttribute("name") + "</a>";
      					if(nodeInfoRecordsSize != (j+1))
      					{ nodesMappedText += ", "; }
      				}
      				serviceUnitRecord.setAttribute("nodesMapped", nodesMappedText);
      				serviceUnitRecord.removeChildren();
      			}
      			tibco.admin.Main.getServer().Cache.setDocument(plugin.serviceUnitsTableCacheId, cacheDoc);
      			
      			// now repaint the table
      			var serviceUnitsTable = objEvent.target.serviceUnitsObj.getDescendantOfName("serviceUnitsTable_MM_dply");
      			serviceUnitsTable.repaintData();
    		};

    		plugin.prototype.ongetServiceUnitsError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetServiceUnitsInvalid = function(objEvent) {
      			jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};

        });